$(document).ready(function() {
	moment.locale("zh-cn");
	window.currentpage = 1;
	ready();
	
});

function ready() {
	queryUserInfo(1);//查询第一页
	//绑定事件
	bind();
	//表单校验
	check();
	showUserName();
}

function bind() {
	$("#pageSize").bind("change",function(){
		queryUserInfo(window.currentpage);
	});
	$("#searchStaffByStaffid").bind("click",function(){
		window.currentpage = 1;
		queryUserInfo(window.currentpage);
	});
	$('#addStaffInfoModal').on('hide.bs.modal', function (){
		$("#insertStaffId").val("");
		$("#insertStaffName").val("");
		$("#insertStaffSex").val("");
		$("#insertStaffId-error").remove();
		$("#insertStaffName-error").remove();
		$("#insertStaffSex-error").remove();
	});
	$('#updateStaffInfoModal').on('hide.bs.modal', function (){
		$("#updateStaffId").val("");
		$("#updateStaffName").val("");
		$("#updateStaffSex").val("");
		$("#updateStaffId-error").remove();
		$("#updateStaffName-error").remove();
		$("#updateStaffSex-error").remove();
	});
	$('#updatePasswordModel').on('hide.bs.modal', function (){
		$("#oldPassword").val("");
		$("#password").val("");
		$("#password_again").val("");
		$("#oldPassword-error").remove();
		$("#password-error").remove();
		$("#password_again-error").remove();
	});
//	$('input[name="selectAllStaff"]').bind("click",function(){
//		if($(this).is(':checked')){
//			$('input[name="selectStaff"]').each(function(){   
//                $(this).prop("checked",true);  
//            });
//		}else{
//			$('input[name="selectStaff"]').each(function(){   
//                $(this).removeAttr("checked",false);
//            });
//		}
//	});
}

function check(){
	addCheck();
	updateCheck();
	updatePasswordCheck();
}


function addCheck(){
	//添加校验
	$("#addStaffInfo").validate({
		rules:{
			insertStaffId:{
				required:true,
				digits:true,
				minlength:6,
				maxlength:6
			},
			insertStaffName:{
				required:true,
			},
			
			insertStaffSex:{
				required:true
			}
		},
		messages:{
			insertStaffId:{
				required:"请输入学号",
				digits:"请输入正确格式的学号",
				minlength:"请输入正确格式的学号",
				maxlength:"请输入正确格式的学号"
			},
			insertStaffName:{
				required:"请输入姓名",
			},
			
			insertStaffSex:{
				required:"请选择性别"
			}
		},
		submitHandler:function(form){
			var staffId = $("#insertStaffId").val();
			var staffGrade = $("#insertStaffGrade").val();
			var staffName = $("#insertStaffName").val();
			var staffSex = $("#insertStaffSex").val();
			$.ajax({
				type:'post',
				url:'insertStaffInfo',
				data:{"staffId":staffId,"staffGrade":staffGrade,"staffName":staffName,"staffSex":staffSex},
				success:function(data){
					queryUserInfo(window.currentpage);
				},
				error:function(){
					console.log("1");
				}
			});
			$('#addStaffInfoModal').modal('hide');
		},
		invalidHandler: function(form, validator) {return false;}
	})
}

function updateCheck(){
	//修改校验
	$("#updateStaffInfo").validate({
		rules:{
			updateStaffId:{
				required:true,
				digits:true,
				minlength:6,
				maxlength:6
			},
			updateStaffName:{
				required:true,
			},
			
			insertStaffSex:{
				required:true
			}
		},
		messages:{
			updateStaffId:{
				required:"请输入学号",
				digits:"请输入正确格式的学号",
				minlength:"请输入正确格式的学号",
				maxlength:"请输入正确格式的学号"
			},
			updateStaffName:{
				required:"请输入姓名",
			},
			
			updateStaffSex:{
				required:"请选择性别"
			}
		},
		submitHandler:function(form){
			var staffId = $("#updateStaffId").val();
			var staffGrade = $("#updateStaffGrade").val();
			var staffName = $("#updateStaffName").val();
			var staffSex = $("#updateStaffSex").val();
			$.ajax({
				type:'post',
				url:'updateStaffInfoBySuper',
				data:{"staffId":staffId,"staffGrade":staffGrade,"staffName":staffName,"staffSex":staffSex},
				success:function(data){
					queryUserInfo(window.currentpage);
				},
				error:function(){
					console.log("1");
				}
			});
			$('#updateStaffInfoModal').modal('hide');
		},
		invalidHandler: function(form, validator) {return false;}
	})
}

function updatePasswordCheck(){
	$("#updatePassword").validate({
		rules:{
			oldPassword:{
				required:true
			},
			password:{
				required:true
			},
			password_again:{
				required:true,
				equalTo: "#password"
			}
		},
		messages:{
			oldPassword:{
				required:"请输入旧密码"
			},
			password:{
				required:"请输入新密码"
			},
			password_again:{
				required:"请输入新密码",
				equalTo: "两次输入不相同"
			}
		},
		submitHandler:function(form){
			var oldPassword = $("#oldPassword").val();
			var password = $("#password").val();
			$.ajax({
				type:'post',
				url:'updatePassword',
				data:{"oldPassword":oldPassword,"password":password},
				success:function(data){
				},
				error:function(){
					console.log("1");
				}
			});
			$('#updatePasswordModel').modal('hide');
		},
		invalidHandler: function(form, validator) {return false;}
	})
}


function queryUserInfo(page){
	var currPage = page;
	var pageSize = $("#pageSize").val();
	var staffStatus = $("#staffStatus").val();
	var staffId = $("#staffId").val();
	$.ajax({
		url:"/dormms/super/queryStaffInfoByPage",
        type:"post",
        data:{"currPage":currPage,"pageSize":pageSize,"staffId":staffId,"isLeave":staffStatus},
        success:function(data){
        	console.log(data);
        	$("#staffInfoList").empty();
        	var sex=["男","女"];
        	$("#total").text("共有"+data.total+"条记录");
        	for(var i=0; i<data.list.length; i++){
        		 var trTemp = $("<tr></tr>");
        		 //trTemp.append("<td>"+ "<input type='checkbox' name='selectstaff' value='" + data.list[i].staffId + "'></td>");
        		 trTemp.append("<td id='findStaffId'>"+ data.list[i].staffId +"</td>");
        		 trTemp.append("<td>"+ data.list[i].staffName +"</td>");
        		 trTemp.append("<td>"+ sex[data.list[i].staffSex-1] +"</td>");
        		 if(data.list[i].staffTel != null){
        			 trTemp.append("<td>"+ data.list[i].staffTel +"</td>");
        		 }
        		 else{
        			 trTemp.append("<td></td>");
        		 }
        		 trTemp.append("<td>" + "<button type='button' class='btn btn-outline-success btn-sm' onclick='getStaffInfoGet(this)'>查看</button>" 
        				 +"<button type='button' class='btn btn-outline-primary btn-sm' onclick='getStaffInfoUpdate(this)'>修改</button>" 
        				 + "<button type='button' class='btn btn-outline-danger btn-sm' onclick='deleteStaff(this)'>离职</button>" 
        				 + "</td>");
        		 trTemp.appendTo("#staffInfoList");
        	}
        	queryUserRoleByPage(data);
        }
	});
}

function queryUserRoleByPage(data) {
	var options = {
			bootstrapMajorVersion: 3,
			currentPage: data.pageNum,
            totalPages: data.pages,
            numberofPages: 5,
            itemTexts: function(type, page, current) { //修改显示文字
                switch (type) {
                case "first":
                    return "&laquo;";
                case "prev":
                    return "&lt;";
                case "next":
                    return "&gt;";
                case "last":
                    return "&raquo;";
                case "page":
                    return page;
                }
            },
            onPageClicked: function (event, originalEvent, type, page) { //异步换页
            	queryUserInfo(page);
            	window.currentpage=page;
            },
	};
	$('#page').bootstrapPaginator(options); 
}


//获取当前行数据(修改)
function getStaffInfoUpdate(obj) {
	var x = $(obj).parent().parent().find("td");
	var staffinfo = x.eq(0).text();
	$.ajax({
		url:"/dormms/super/queryStaffInfoByStaffId",
		type:"get",
		data:{'staffId':staffinfo},
		success:function(data){
			console.log(data);
			$("#updateStaffId").val(data.staffId);
			$("#updateStaffId").attr("readonly","readonly");
			$("#updateStaffName").val(data.staffName);
			$("#updateStaffSex").val(data.staffSex);
			$('#updateStaffInfoModal').modal('show');
		}
	});
}
//获取当前行数据(查看)
function getStaffInfoGet(obj) {
	var x = $(obj).parent().parent().find("td");
	var staffinfo = x.eq(0).text();
	$.ajax({
		url:"/dormms/super/queryStaffInfoByStaffId",
		type:"get",
		data:{'staffId':staffinfo},
		success:function(data){
			console.log(data);
			$("#getStaffId").val(data.staffId);
			$("#getStaffName").val(data.staffName);
			$("#getStaffSex").val(data.staffSex);
			$("#getStaffGrade").val(data.staffGrade);
			console.log(data.staffBirthday);
			if(data.staffBirthday!=null){
				$("#getStaffBirthday").val(moment(data.staffBirthday).format('L'));
			}else{
				$("#getStaffBirthday").val("");
			}
			$("#getStaffTel").val(data.staffTel);
			$("#getStaffParentTel").val(data.staffParentTel);
			$('#getStaffInfoModal').modal('show');
		}
	});
}

//删除
function deleteStaff(obj){
	var x = $(obj).parent().parent().find("td");
	var staffId = x.eq(0).text();
	$.ajax({
		url:"deleteStaffInfo",
		type:"get",
		data:{'staffId':staffId},
		success:function(data){
			queryUserInfo(window.currentpage);
		}
	});
}

function showPasswordModel() {
	$('#updatePasswordModel').modal('show');
}

function showUserName() {
	$.ajax({
		url:"getSuperInfo",
		type:"get",
		success:function(data){
			$("#userId").text(data.userId);
		}
	});
}
